/*
 * iSnakeConfiguration.java
 *
 * Created on January 6, 2007, 8:52 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.sf.iSnake.configuration;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

/**
 *
 * @author thelinuxmaniac
 * @TODO Implement the retrival and saving of game options using "JWS: BasicServices for saving user preferences"
 *
 */
public class iSnakeConfiguration {
    
    private ClassLoader classLoader;        // ClassLoader which is required to access local resources in JWS
    
    public  static Short GAME_MULTIPLAYER_SERVER_MODE   = 1;
    public  static Short GAME_MULTIPLAYER_CLIENT_MODE   = 2;
    public  static Short GAME_STANDALONE_MODE           = 3;
    
    private Short gameMode;          // mode of the game executing
    
    private Image iSnakeLogo;               // Logo of iSnake which is shown when application is launched
    private Image iSnakeLogoSmall;          // Small Logo of iSnake which is shown at the corner of each Panel
    private Image imageNext;                // Right arrow (next) image
    private Image iSnakeIcon;               // iSnake icon
    
    /**
     * Path for images and other resource required for the application
     */
    private static String iSnakeIconPath = "images/iSnake_icon.jpg";
    private static String iSnakeLogoPath = "images/iSnake_logo.jpg";
    private static String iSnakeLogoSmallPath = "images/iSnake_logo_small.jpg";
    private static String imageNextPath = "images/next.png";
    
    /**
     * String settings
     */
    private static String iSnakeTitle = "iSnake";
    public static String iStringFont = "Monospaced";
    public static Integer iStringFontSize = 12;
    
    /**
     * Configuration options for MenuItem frame
     */
    public static String MenuItemsFont = "Monospaced";
    public static Integer MenuItemsFontSize = 12;
    public static Color MenuItemForegroundColor = Color.WHITE;
    public static Color MenuItemBackgroundColor = Color.BLACK;
    
    /**
     * Configuration options for Multiplayer Mode of game play
     */
    public static String MultiplayerModeFont = "Monospaced";
    public static Integer MultiplayerModeFontSize = 12;
    public static String MultiplayerModeHeaderFont = "Monospaced";
    public static Integer MultiplayerModeHeaderFontSize = 24;

    public static Color MultiplayerModeForegroundColor = Color.WHITE;
    public static Color MultiplayerModeBackgroundColor = Color.BLACK;
    public static Color MultiplayerModeHeaderColor = Color.LIGHT_GRAY;
    
    /**
     * Configuration options for Standalone Mode of game play
     */
    public static String StandaloneModeFont = "Monospaced";
    public static Integer StandaloneModeFontSize = 12;
    public static String StandaloneModeHeaderFont = "Monospaced";
    public static Integer StandaloneModeHeaderFontSize = 24;
    
    public static Color StandaloneModeForegroundColor = Color.WHITE;
    public static Color StandaloneModeBackgroundColor = Color.BLACK;
    public static Color StandaloneModeHeaderColor = Color.LIGHT_GRAY;
    
    /**
     * Configuration options for Game Controller Panel
     */
    public static String GameControllerPanelFont = "Monospaced";
    public static Integer GameControllerPanelFontSize = 12;
    
    public static String ChatMessageAreaFont = "Monospaced";
    public static Integer ChatMessageAreaFontSize = 10;
        
    public static Color GameControllerForegroundColor = Color.WHITE;
    public static Color GameControllerBackgroundColor = new Color(181,204,213);
    
    public static Color GameObjectColorWall = Color.YELLOW;
    public static Color GameObjectColorCFood = Color.PINK;
    public static Color GameObjectColorNFood = Color.GRAY;
    public static Color GameObjectColorNULL = Color.BLACK;

    public static Integer GameFieldPanelPadX = 10;
    public static Integer GameFieldPanelPadY = 10;
    // The title bar is of height 14px. Hence we increase the Dimension height by 14
    public static Dimension GameFieldPanelDimension = new Dimension(640+GameFieldPanelPadX*4,480+GameFieldPanelPadY*4+14);   // Considering padding on both sides
    public static Dimension GameStatusPanelDimension = new Dimension(160-GameFieldPanelPadX*4,480+GameFieldPanelPadY*4+14);  // Considering padding on both sides
    public static Dimension GameMessagePanelDimension = new Dimension(800,120-GameFieldPanelPadY*4-14);
    
    public static Dimension scoreAndLifePanelDimension = new Dimension(160-GameFieldPanelPadX*4,200);
    public static Dimension informationPanelDimension = new Dimension(160-GameFieldPanelPadX*4,480+GameFieldPanelPadY*4+14-200);
    
    /**
     * Several Frame dimensions definations
     */
    private static Dimension DesiredFrameDimension = new Dimension(800,600);
    private static Dimension MaximumFrameDimension = new Dimension(1280,1024);
    private static Dimension MinimumFrameDimension = new Dimension(800,600);
    private static Dimension GameContentPanelDimension;
    
    /**
     * Initial Application Window placement position on the screen
     */
    private Integer frameLocationX;
    private Integer frameLocationY;
    
    /**
     * Game Area width and height
     */
    public static Short GAME_AREA_WIDTH = 79;     // 0 to 79; total 80
    public static Short GAME_AREA_HEIGHT = 39;    // 0 to 39; total 40

    /**
     * iSnakeClient configurations. The name and port of game server to which this client is going to connect
     */
    private Integer serverPortForClient = 9669;
    private String serverNameForClient = "localhost";
    
    /**
     * Color definations for different UI components
     */
    public static Color labelColor = Color.WHITE;
    
    public static Color textFieldForegroundColor = Color.WHITE;
    public static Color textFieldBackgroundColor = Color.BLACK;
    public static Color textFieldFocusColor = new Color(126,129,155);
    public static Color textFieldBorderColor = Color.LIGHT_GRAY;
    
    public static Color comboBoxForegroundColor = Color.WHITE;
    public static Color comboBoxBackgroundColor = Color.BLACK;
    public static Color comboBoxFocusColor = new Color(126,129,155);
    public static Color comboBoxBorderColor = Color.LIGHT_GRAY;

    public static Color PanelChangeButtonFGColor = Color.WHITE;
    public static Color PanelChangeButtonBGColor = Color.BLACK;
    
    public static Color PanelActionButtonFGColor = Color.WHITE;
    public static Color PanelActionButtonBGColor = Color.LIGHT_GRAY;
    
    public static Color ScoreColor = Color.YELLOW;
    public static Integer ScoreFontSize = 24;
    public static String ScoreFont = "Monospaced";
    
    /**
     * Creates a new instance of iSnakeClientConfiguration
     */
    public iSnakeConfiguration(ClassLoader c) {
        this.classLoader = c;
        this.setGameMode(GAME_MULTIPLAYER_CLIENT_MODE);     // Default game mode is Multiplayer Client Mode
        
        URL iSnakeIconURL = getClassLoader().getResource(iSnakeIconPath);
        URL iSnakeLogoURL = getClassLoader().getResource(iSnakeLogoPath);
        URL iSnakeLogoSmallURL = getClassLoader().getResource(iSnakeLogoSmallPath);
        URL imageNextURL = getClassLoader().getResource(imageNextPath);
        
        try {
            setISnakeLogo(ImageIO.read(iSnakeLogoURL));
            setImageNext(ImageIO.read(imageNextURL));
            setISnakeIcon(ImageIO.read(iSnakeIconURL));
            setISnakeLogoSmall(ImageIO.read(iSnakeLogoSmallURL));
        } catch (IOException ex) {
            System.err.println("Error loading image resources");
            ex.printStackTrace();
        }
        
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        setFrameLocationX((screenSize.width - DesiredFrameDimension.width)/2);
        setFrameLocationY((screenSize.height - DesiredFrameDimension.height)/2);        
    }

    /**
     * Returns a JLabel according to predefined style so that the items in the UI looks uniform
     */
    public JLabel getLabelForUIPanel(JLabel label)  {
        label.setForeground(labelColor);
        return label;
    }
    
    /**
     * Returns a JTextField according to predefined style so that the items in the UI looks uniform
     */
    public JTextField getTextFieldForUIPanel(JTextField textField)  {
        textField.setForeground(textFieldForegroundColor);
        textField.setBackground(textFieldBackgroundColor);
        textField.setBorder(new LineBorder(textFieldBorderColor,1));
        textField.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
                ((JTextField) e.getSource()).setBackground(textFieldFocusColor);
            }
            public void focusLost(FocusEvent e) {
                ((JTextField) e.getSource()).setBackground(textFieldBackgroundColor);
            }
        });
        return textField;
    }
    
    /**
     * Returns a JComboBox according to predefined style so that the items in the UI looks uniform
     */
    
    public JComboBox getComboBoxForUIPanel(JComboBox comboBox)  {
        comboBox.setBackground(comboBoxBackgroundColor);
        comboBox.setBorder(new LineBorder(comboBoxBorderColor,1));
        comboBox.setForeground(comboBoxForegroundColor);
        comboBox.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
                ((JComboBox) e.getSource()).setBackground(comboBoxFocusColor);
            }
            public void focusLost(FocusEvent e) {
                ((JComboBox) e.getSource()).setBackground(comboBoxBackgroundColor);
            }
        });
        return comboBox;
    }
    
    // Change this to match the UI color scheme
    public JButton getPanelChangeButtonForUIPanel(JButton button)  {
        button.setForeground(PanelChangeButtonFGColor);
        button.setBackground(PanelChangeButtonBGColor);
        button.setBorder(null);
        button.setBorderPainted(false);
        button.setContentAreaFilled(true);
        button.setRolloverEnabled(false);
        return button;
    }
    
    public JButton getPanelActionButtonForUIPanel(JButton button)  {
        button.setForeground(Color.LIGHT_GRAY);
        button.setBackground(Color.BLACK);
        button.setBorder(new LineBorder(Color.GRAY,1));
        button.setBorderPainted(true);
        button.setContentAreaFilled(false);
        button.setRolloverEnabled(false);
        return button;
    }

    public ClassLoader getClassLoader() {
        return classLoader;
    }

    private void setClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    public Image getISnakeLogo() {
        return iSnakeLogo;
    }

    private void setISnakeLogo(Image iSnakeLogo) {
        this.iSnakeLogo = iSnakeLogo;
    }

    public Image getImageNext() {
        return imageNext;
    }

    private void setImageNext(Image imageNext) {
        this.imageNext = imageNext;
    }

    public Image getISnakeIcon() {
        return iSnakeIcon;
    }

    private void setISnakeIcon(Image iSnakeIcon) {
        this.iSnakeIcon = iSnakeIcon;
    }

    public static String getISnakeTitle() {
        return iSnakeTitle;
    }

    private static void setISnakeTitle(String aISnakeTitle) {
        iSnakeTitle = aISnakeTitle;
    }

    public static Dimension getDesiredFrameDimension() {
        return DesiredFrameDimension;
    }

    public static void setDesiredFrameDimension(Dimension aDesiredFrameDimension) {
        DesiredFrameDimension = aDesiredFrameDimension;
    }

    public static Dimension getMaximumFrameDimension() {
        return MaximumFrameDimension;
    }

    public static void setMaximumFrameDimension(Dimension aMaximumFrameDimension) {
        MaximumFrameDimension = aMaximumFrameDimension;
    }

    public static Dimension getMinimumFrameDimension() {
        return MinimumFrameDimension;
    }

    public static void setMinimumFrameDimension(Dimension aMinimumFrameDimension) {
        MinimumFrameDimension = aMinimumFrameDimension;
    }

    public Integer getFrameLocationX() {
        return frameLocationX;
    }

    public void setFrameLocationX(Integer frameLocationX) {
        this.frameLocationX = frameLocationX;
    }

    public Integer getFrameLocationY() {
        return frameLocationY;
    }

    public void setFrameLocationY(Integer frameLocationY) {
        this.frameLocationY = frameLocationY;
    }

    public static Dimension getGameContentPanelDimension() {
        return GameContentPanelDimension;
    }

    public static void setGameContentPanelDimension(Dimension aGameContentPanelDimension) {
        GameContentPanelDimension = aGameContentPanelDimension;
    }

    public Image getISnakeLogoSmall() {
        return iSnakeLogoSmall;
    }

    public void setISnakeLogoSmall(Image iSnakeLogoSmall) {
        this.iSnakeLogoSmall = iSnakeLogoSmall;
    }

    public Short getGameMode() {
        return gameMode;
    }

    public void setGameMode(Short gameMode) {
        this.gameMode = gameMode;
    }

    public Integer getServerPortForClient() {
        return serverPortForClient;
    }

    public void setServerPortForClient(Integer serverPortForClient) {
        this.serverPortForClient = serverPortForClient;
    }

    public String getServerNameForClient() {
        return serverNameForClient;
    }

    public void setServerNameForClient(String serverNameForClient) {
        this.serverNameForClient = serverNameForClient;
    }
}